﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigureInstancePageViewModel.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the ConfigureInstancePageViewModel type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch.ViewModel
{
    using System;
    using System.ComponentModel;
    using System.ServiceModel.DomainServices.Client;
    using System.Windows;
    using System.Windows.Input;

    using MySemanticSearch.Commands;
    using MySemanticSearch.Model;
    using MySemanticSearch.View;
    using MySemanticSearch.Web.DataAccess;

    /// <summary>
    /// ViewModel for the Configure SQL instance dialog
    /// </summary>
    public class ConfigureInstancePageViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Dialog View
        /// </summary>
        private readonly IInstanceView dialogView;

        /// <summary>
        /// SQL Server Management Domain Context
        /// </summary>
        private readonly SqlServerManagement sqlServerManagement = new SqlServerManagement();
        
        /// <summary>
        /// SQL Instance  Name
        /// </summary>
        private string sqlInstanceName;

        /// <summary>
        /// Error Message
        /// </summary>
        private string errorMessage;

        /// <summary>
        /// Indicates whether the View is enabled
        /// </summary>
        private bool isPageEnabled;

        /// <summary>
        /// Connection status string
        /// </summary>
        private string connectionStatus;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigureInstancePageViewModel"/> class.
        /// </summary>
        /// <param name="view">
        /// The dialog view.
        /// </param>
        public ConfigureInstancePageViewModel(IInstanceView view)
        {
            this.dialogView = view;
            this.IsPageEnabled = true;
        }

        /// <summary>
        /// Property changed handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        
        /// <summary>
        /// Gets or sets SQL Instance Name.
        /// </summary>
        public string SqlInstanceName
        {
            get
            {
                return this.sqlInstanceName;
            }

            set
            {
                this.sqlInstanceName = value;
                this.NotifyPropertyChanged("SqlInstanceName");
            }
        }

        /// <summary>
        /// Gets or sets ConnectionStatus.
        /// </summary>
        public string ConnectionStatus
        {
            get
            {
                return this.connectionStatus;
            }

            set
            {
                this.connectionStatus = value;
                this.NotifyPropertyChanged("ConnectionStatus");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether IsPageEnabled.
        /// </summary>
        public bool IsPageEnabled
        {
            get
            {
                return this.isPageEnabled;
            }

            set
            {
                this.isPageEnabled = value;
                this.NotifyPropertyChanged("IsPageEnabled");
            }
        }

        /// <summary>
        /// Gets or sets ErrorMessage.
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return this.errorMessage;
            }

            set
            {
                this.errorMessage = value;
                this.NotifyPropertyChanged("ErrorMessage");
            }
        }

        /// <summary>
        /// Gets AddSQLInstanceCommand.
        /// </summary>
        public ICommand AddSqlInstanceCommand
        {
            get
            {
                return new RelayCommand(this.AddSqlInstance);
            }
        }

        /// <summary>
        /// Add SQL Instance method
        /// </summary>
        public void AddSqlInstance()
        {
            if (string.IsNullOrEmpty(this.SqlInstanceName))
            {
                return;
            }

            this.ConnectionStatus = Application.Current.Resources["ConnectionInProgress"].ToString();
            this.IsPageEnabled = false;
            this.sqlServerManagement.CheckServerConnection(this.SqlInstanceName, this.OnConnectionSuccess, null);
        }

        /// <summary>
        /// Uses the RIA service to check connection string
        /// </summary>
        /// <param name="args">
        /// The args param.
        /// </param>
        private void OnConnectionSuccess(OperationBase args)
        {
            this.IsPageEnabled = true;

            if (args.HasError)
            {
                this.ConnectionStatus = Application.Current.Resources["ConnectionFailed"].ToString();
                args.MarkErrorAsHandled();
                var errorDialog = new ErrorDialogPage(args.Error.Message);
                errorDialog.Show();
            }
            else
            {
                var eventArgs = new SqlInstanceSettingsEventArgs(this.SqlInstanceName);
                this.dialogView.InvokeOnClose(eventArgs);
            }
        }

        #region Notify Property Changed
        /// <summary>
        /// Notify property Changed
        /// </summary>
        /// <param name="info">
        /// The info string.
        /// </param>
        private void NotifyPropertyChanged(string info)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
